package com.koron.css2.baseConfig.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MatrCodeBean;
import com.koron.css2.baseConfig.bean.UseMatrTemplateListBean;
import com.koron.css2.baseConfig.mapper.UseMatrTemplateMapper;
import com.koron.css2.baseConfig.queryBean.UseMatrTemplateQueryBean;
import com.koron.css2.baseConfig.vo.MatrTemplateSelectVO;
import com.koron.css2.baseConfig.vo.UseMatrTemplateVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.MatrCodeMapper;
import com.koron.util.Constant;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 领料模板下拉框
 * @author 许海宜
 * 2020年3月31日
 */
public class GetMatrTemplateSelect implements ServerInterface  {
	
	private static Logger logger = LogManager.getLogger(GetMatrTemplateSelect.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		
		@SuppressWarnings("rawtypes")
		MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", List.class);
		try {
			//获取mapper实例
			MatrCodeMapper matrMapper = factory.getMapper(MatrCodeMapper.class, "_default");
			UseMatrTemplateMapper mapper = factory.getMapper(UseMatrTemplateMapper.class);
			
			List<MatrTemplateSelectVO> res = new ArrayList<MatrTemplateSelectVO>();
			//查询模板列表
			UseMatrTemplateQueryBean matrTempQueryBean = new UseMatrTemplateQueryBean();
			matrTempQueryBean.setStatus(1);
			List<UseMatrTemplateVO> list = mapper.selectUseMatrTemplate(matrTempQueryBean);
			for (UseMatrTemplateVO vo : list) {
				///查询模板列表
				List<UseMatrTemplateListBean> matrs = mapper.getMatrTemplateListByTemplateId(vo.getId());
				for (UseMatrTemplateListBean m : matrs) {
					//查询材料信息
					MatrCodeBean matr = matrMapper.selectMatrTemplateByNo(m.getMatrNo());
					if(matr != null) {
						m.setMatrId(matr.getId());
						m.setMatrName(matr.getMatrName());
						m.setMatrMode(matr.getMatrMode());
						m.setMatrUnit(matr.getMatrUnit());
						//判断定价表为空取默认值
						if(m.getMatrPrice() == null || m.getMatrPrice() == 0) {
							m.setMatrPrice(matr.getMatrPrice());
						}
						m.setMatrMoney(m.getMatrPrice()*m.getMatrNum());
					}
				}
				MatrTemplateSelectVO selectVO = new MatrTemplateSelectVO();
				selectVO.setTemplateId(vo.getId());
				selectVO.setTemplateName(vo.getTemplateName());
				selectVO.setMatrs(matrs);
				res.add(selectVO);
			}
			//输出结果
			info.setData(res);
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}